package cn.janescott.inner.space.lib.web.security;

import cn.janescott.inner.space.lib.boot.properties.SpringBootAdminProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import java.util.Arrays;

/**
 * 用于处理springboot admin 白名单的处理器
 * Created by Scott on 2018/7/20
 */
public class SpringBootAdminWhiteListHandler {

    private static Logger logger = LoggerFactory.getLogger(SpringBootAdminWhiteListHandler.class);

    /**
     * 将admin的路径白名单加到spring security
     */
    public static void handle(HttpSecurity http, SpringBootAdminProperties properties) throws Exception {
        if (null != properties) {
            String whiteList = properties.getWhiteList();
            if (!StringUtils.isEmpty(whiteList)) {
                String[] urls = whiteList.split(",");
                if (logger.isInfoEnabled()) {
                    logger.info("-------------- spring boot admin white list handler add white url-patterns: [" + Arrays.toString(urls) + "]------------");
                }
                http.authorizeRequests().antMatchers(urls).permitAll();
            }
        }
    }
}
